package com.xpqh.ksg.modular.auth.controller;


import com.xpqh.ksg.base.BaseController;
import com.xpqh.ksg.base.BaseResponseDTO;
import com.xpqh.ksg.common.enums.result.BizExceptionEnum;
import com.xpqh.ksg.common.enums.result.BizSuccessEnum;
import com.xpqh.ksg.common.persistence.dao.KsgCaseInfoMapper;
import com.xpqh.ksg.common.utils.JwtTokenUtil;
import com.xpqh.ksg.modular.auth.dto.AuthPhoneLoginReqDTO;
import com.xpqh.ksg.modular.auth.dto.AuthRespDTO;
import com.xpqh.ksg.modular.auth.dto.KsgAccountPasswordReqDTO;
import com.xpqh.ksg.modular.auth.dto.KsgVerificationRespDTO;
import common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping(value = "ksg")
public class KsgAccountPasswordController extends BaseController {

    @Autowired
    private KsgCaseInfoMapper caseInfoMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @GetMapping("/accountPassword")
    public BaseResponseDTO get(@Valid @RequestBody KsgAccountPasswordReqDTO req) throws Exception {
        try {
            KsgVerificationRespDTO userInfo = caseInfoMapper.accountPassword(req.getAccount(),req.getPassword());
            if (userInfo == null) {
                return renderResult(BizExceptionEnum.AUTH_REQUEST_ERROR);
            }
            if (!StringUtils.isNotBlank(userInfo.getIsStorage())) {
                userInfo.setIsStorage("0");
            }
            String randomKey = jwtTokenUtil.getRandomKey();
            String token = jwtTokenUtil.generateToken(userInfo.getUserId(), randomKey);
            userInfo.setRandomKey(randomKey);
            userInfo.setToken("Bearer " + token);
            if (null == userInfo.getUserLevel()) {
                userInfo.setUserLevel(0);
            }
            return renderResultOk(userInfo);
        }catch (Exception e){
            e.printStackTrace();
            return renderResult(BizExceptionEnum.EYI);
        }
    }
}
